#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>

#define SERVER_PORT  8080
#define SERVER_ADDR	 "192.168.0.104"

int main(int argc, char *argv[])
{
	int ret;
	int sockfd;
	struct sockaddr_in serveraddr = {0};
	char buf[512];

	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) {
		printf("socket error\n");
		exit(EXIT_FAILURE);
	}

	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(SERVER_PORT);
	//serveraddr.sin_addr.s_addr = htonl(SERVER_ADDR);
	inet_pton(AF_INET, SERVER_ADDR, &serveraddr.sin_addr);

	ret = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
	if (ret < 0) {
		printf("connet error\n");
		close(sockfd);
		exit(EXIT_FAILURE);
	}
	
	while (1) {
		memset(buf, 0, sizeof(buf));

		printf("输入要发送的内容：\n");
		fgets(buf, sizeof(buf), stdin);
		
		ret = send(sockfd, buf, strlen(buf), 0);
		if (ret < 0) {
			printf("send error");
		}

		if (strncmp(buf, "exit", 4) == 0) {
			break;
		}
	}

	close(sockfd);
	exit(EXIT_SUCCESS);
}

